/**
 * 演示数据
 * 用于测试医院数字孪生系统功能
 */

import { hospitalDB } from './indexedDB'

export const initDemoData = async () => {
  try {
    // 检查是否已有数据
    const hospitals = await hospitalDB.getHospitals()
    if (hospitals.length > 0) {
      console.log('演示数据已存在，跳过初始化')
      return
    }

    console.log('正在初始化演示数据...')

    // 创建演示医院
    const demoHospital = await hospitalDB.addHospital({
      name: '杭州市第一人民医院',
      address: '浙江省杭州市西湖区',
      center: [120.1551, 30.2741],
      bounds: {
        north: 30.2791,
        south: 30.2691,
        east: 120.1601,
        west: 120.1501
      },
      floors: ['B2', 'B1', 'L1', 'L2', 'L3', 'L4', 'L5'],
      metadata: {
        type: '三甲医院',
        bedCount: 1200,
        established: '1954年'
      }
    })

    console.log('医院创建成功:', demoHospital.name)

    // 创建演示建筑
    const mainBuilding = await hospitalDB.addHospitalBuilding({
      hospitalId: demoHospital.id,
      name: '门诊大楼',
      description: '主要门诊科室和医疗服务',
      center: [120.1551, 30.2741],
      bounds: {
        north: 30.2751,
        south: 30.2731,
        east: 120.1561,
        west: 120.1541
      },
      modelPath: '/models/hospital_main.glb',
      floors: ['L1', 'L2', 'L3', 'L4', 'L5'],
      metadata: {
        buildingType: '门诊楼',
        floors: 5,
        area: '15000平方米'
      }
    })

    const inpatientBuilding = await hospitalDB.addHospitalBuilding({
      hospitalId: demoHospital.id,
      name: '住院部',
      description: '住院病房和护理服务',
      center: [120.1561, 30.2751],
      bounds: {
        north: 30.2761,
        south: 30.2741,
        east: 120.1571,
        west: 120.1551
      },
      modelPath: '/models/hospital_inpatient.glb',
      floors: ['L1', 'L2', 'L3', 'L4', 'L5', 'L6', 'L7', 'L8'],
      metadata: {
        buildingType: '住院楼',
        floors: 8,
        bedCount: 800
      }
    })

    console.log('建筑创建成功:', mainBuilding.name, inpatientBuilding.name)

    // 创建演示科室
    const departments = [
      {
        name: '急诊科',
        type: 'emergency' as const,
        position: [120.1551, 30.2741] as [number, number],
        description: '24小时急诊医疗服务',
        contact: {
          phone: '0571-87236114',
          hours: '24小时',
          director: '张医生'
        },
        capacity: 50
      },
      {
        name: '内科',
        type: 'outpatient' as const,
        position: [120.1552, 30.2742] as [number, number],
        description: '内科疾病诊疗',
        contact: {
          phone: '0571-87236115',
          hours: '8:00-17:30',
          director: '李医生'
        },
        capacity: 30
      },
      {
        name: '外科',
        type: 'surgery' as const,
        position: [120.1553, 30.2743] as [number, number],
        description: '外科手术和诊疗',
        contact: {
          phone: '0571-87236116',
          hours: '8:00-17:30',
          director: '王医生'
        },
        capacity: 20
      },
      {
        name: '儿科',
        type: 'outpatient' as const,
        position: [120.1554, 30.2744] as [number, number],
        description: '儿童医疗保健服务',
        contact: {
          phone: '0571-87236117',
          hours: '8:00-17:30',
          director: '陈医生'
        },
        capacity: 25
      },
      {
        name: '妇产科',
        type: 'inpatient' as const,
        position: [120.1555, 30.2745] as [number, number],
        description: '妇女儿童医疗服务',
        contact: {
          phone: '0571-87236118',
          hours: '8:00-17:30',
          director: '赵医生'
        },
        capacity: 40
      }
    ]

    for (const dept of departments) {
      await hospitalDB.addDepartment({
        hospitalId: demoHospital.id,
        buildingId: mainBuilding.id,
        ...dept,
        status: 'active',
        metadata: {}
      })
    }

    console.log('科室创建成功，共', departments.length, '个科室')

    // 创建演示医疗设备
    const equipment = [
      {
        name: 'CT扫描仪-1',
        type: 'ct' as const,
        position: [120.1556, 30.2746] as [number, number],
        description: '高精度CT扫描设备',
        specifications: '64排螺旋CT',
        manufacturer: '西门子'
      },
      {
        name: 'MRI核磁共振-1',
        type: 'mri' as const,
        position: [120.1557, 30.2747] as [number, number],
        description: '1.5T磁共振成像设备',
        specifications: '1.5T超导磁体',
        manufacturer: '飞利浦'
      },
      {
        name: 'X光机-1',
        type: 'xray' as const,
        position: [120.1558, 30.2748] as [number, number],
        description: '数字化X射线摄影设备',
        specifications: 'DR数字化X光机',
        manufacturer: '锐珂'
      },
      {
        name: 'B超设备-1',
        type: 'ultrasound' as const,
        position: [120.1559, 30.2749] as [number, number],
        description: '彩色多普勒超声设备',
        specifications: '彩超四维成像',
        manufacturer: 'GE'
      },
      {
        name: '除颤器-1',
        type: 'defibrillator' as const,
        position: [120.1560, 30.2750] as [number, number],
        description: '自动体外除颤器',
        specifications: 'AED自动除颤',
        manufacturer: '迈瑞'
      }
    ]

    for (const equip of equipment) {
      await hospitalDB.addMedicalEquipment({
        hospitalId: demoHospital.id,
        ...equip,
        status: 'active',
        metadata: {}
      })
    }

    console.log('医疗设备创建成功，共', equipment.length, '个设备')

    console.log('演示数据初始化完成！')
    return {
      hospital: demoHospital,
      buildings: [mainBuilding, inpatientBuilding],
      departmentCount: departments.length,
      equipmentCount: equipment.length
    }

  } catch (error) {
    console.error('初始化演示数据失败:', error)
    throw error
  }
}

export const clearDemoData = async () => {
  try {
    console.log('正在清除演示数据...')
    
    // 清除所有数据
    const dbService = (hospitalDB as any).dbService || hospitalDB
    const tables = ['medical_equipment', 'departments', 'hospital_buildings', 'hospitals']
    
    for (const table of tables) {
      try {
        await dbService.clear(table)
      } catch (error) {
        console.log(`清除表 ${table} 时出错:`, error)
      }
    }
    
    console.log('演示数据清除完成！')
  } catch (error) {
    console.error('清除演示数据失败:', error)
    throw error
  }
}